<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/>
  <title>进度</title>

  <div th:replace="common :: common_header"></div>
</head>
<body>
<div th:replace="common::#leftmenu"></div>
<div class="main-content">
  <div class="progress progress-striped active">
    <div id="prog_in" class="progress-bar" role="progressbar"
         aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
    </div>
    <span id="span_out"></span>
  </div>
</div>
</body>
<script type="text/javascript">
  var timer;
  function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
      return unescape(r[2]);
    }
    return null;
  }
  function getAllUrlParam() {
    var url = location.search; //获取url中"?"符后的字串
    var ret = null;
    if (url.indexOf("?") !== -1) {
      // 获取 ？ 后面所有的 key 和 value ，这种方式可以直接放在ajax的data里，
      ret = url.substr(1).split('&').splice(0,).join('&').toLocaleString()
    }
    return ret; //返回参数值
  }
  var pre_url = document.referrer;
  console.log(pre_url);
  var baseUrl = getQueryString("baseUrl");
  console.log(baseUrl);
  var param  = getAllUrlParam();
  console.log(param);
  switch (baseUrl) {
    case "getevent":
      getEvents();
      break;
    case "getrealdata":
      getRealDatas();
      break;
    case "getpara":
      getParas();
      break;
    case "getwavehead":
      getWaveheads();
      break;
    case "getwave":
      getWaves();
      break;
  }

  function getEvents() {
    console.log("获取事件");
    $.ajax({
      url: "/getevent",
      success: function (data) {
        console.log(data);
        if (data==="failed"){
          clearInterval(timer);
          window.location.href="/failed";
        } else if (data==="device") {
          clearInterval(timer);
          window.location.href="/device.html";
        }
      }
    })
  }
  function getRealDatas() {
    console.log("获取定值");
    $.ajax({
      url: "/getrealdata",
      success: function (data) {
        console.log(data);
        if (data==="failed"){
          clearInterval(timer);
          window.location.href="/failed";
        } else if (data==="device") {
          clearInterval(timer);
          window.location.href = "/device.html";
        }
      }
    })
  }
  function getParas() {
    console.log("获取定值");
    $.ajax({
      url: "/getpara",
      success: function (data) {
        console.log(data);
        if (data==="failed"){
          clearInterval(timer);
          window.location.href="/failed";
        } else if (data==="device") {
          clearInterval(timer);
          window.location.href = "/device.html";
        }
      }
    })
  }
  function getWaveheads() {
    console.log("获取录波总览");
    $.ajax({
      url: "/getwavehead",
      success: function (data) {
        console.log(data);
        if (data==="failed"){
          clearInterval(timer);
          window.location.href="/failed";
        } else if (data==="device") {
          clearInterval(timer);
          window.location.href = "/device.html";
        }
      }
    })
  }
  function getWaves() {
    console.log("获取录波");
    $.ajax({
      async: true,
      type: "GET",
      url: "getwave",
      data: param,
      success: function (data) {
        console.log(data);
        if (data==="failed"){
          clearInterval(timer);
          window.location.href="/failed";
        }else if(data==="success"){
          window.location.href="/download?"+param;
        } else if (data==="device") {
          clearInterval(timer);
          window.location.href = "/device.html";
        }
      }
    })
  }
  $(document).ready(function () {
    timer = setInterval(function () {
      $.ajax({
        async: false,
        type: "GET",
        url: baseUrl+"/getprocess",
        dataType: "text",
        success: function (data) {
          console.log(data);
          document.getElementById("span_out").innerText=data+"%";
          $(".progress-bar").css("width", data+"%");
          if (data >= 100){
            clearInterval(timer);
            window.location.href="/success";
          }
        }
      })
    }, 1000);
  });


</script>
</html>